/**
 * Copyright 2007-2010 非也
 * All rights reserved. 
 * 
 * This library is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License v3 as published by the Free Software
 * Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, see http://www.gnu.org/licenses/lgpl.html.
 *
 */
package org.fireflow2.designer.eclipse.figures.widget;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.ImageUtilities;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.TitleBarBorder;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;

/**
 *
 * @author 非也 nychen2000@163.com
 * Fire Workflow 官方网站：www.firesoa.com 或者 www.fireflow.org
 *
 */
public class PoolLaneBorder extends TitleBarBorder {
	int inset = 1;
	Color borderColor = ColorConstants.black;
	public static final int TITLE_BAR_HEIGH = 32;
	public PoolLaneBorder(int inset){
		this.inset = inset;
	}
	public void setBorderColor(Color c){
		this.borderColor = c;
	}

	protected Insets calculateInsets(IFigure figure) {
		return new Insets(inset,getTextExtents(figure).height + getPadding().getWidth(),
				 inset,inset);
	}
	public boolean isOpaque() {
		return false;
	}
	
	public void paint(IFigure figure, Graphics g, Insets insets) {
		tempRect.setBounds(getPaintRectangle(figure, insets));
		Rectangle rec = tempRect;
		Dimension textDimenstion = this.getTextExtents(figure);
//		int titleBarHeight = Math.min(rec.width, textDimenstion.height
//				+ getPadding().getWidth());
		rec.width = TITLE_BAR_HEIGH;
//		g.clipRect(rec);

		int x = rec.x + getPadding().left;
		int y = rec.y + getPadding().top;

		int textWidth = textDimenstion.width;
		int freeSpaceX = rec.width - getPadding().getWidth()-textDimenstion.height;
		
		int freeSpaceY = rec.height - getPadding().getHeight() - textWidth;

		if (getTextAlignment() == PositionConstants.CENTER)
			freeSpaceY /= 2;
		if (getTextAlignment() != PositionConstants.LEFT)
			y += freeSpaceY;
		
		if (getTextAlignment() == PositionConstants.CENTER)
			freeSpaceX /= 2;
		
		if (getTextAlignment() != PositionConstants.LEFT)
			x += freeSpaceX;
		
		Image foo = ImageUtilities.createRotatedImageOfString(getLabel(),
				getFont(figure), this.getTextColor(), ColorConstants.white);
		g.drawImage(foo, x,y);
		foo.dispose();
		
		Color oldColor = g.getForegroundColor();
		g.setForegroundColor(borderColor);
		g.drawLine(new Point(rec.x+rec.width,rec.y), new Point(rec.x+rec.width,rec.y+rec.height));
		g.setForegroundColor(oldColor);
	}
}
